package tr.gov.tubitak.bilgem.esya.certselector;

import tr.gov.tubitak.bilgem.esya.certviewer.ECertViewerDialog;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 11.10.2011
 * Time: 09:27
 * To change this template use File | Settings | File Templates.
 */
public class ReaderCertListTree extends JTree {
    ReaderCertListTree(){
        super();
        initGUI();
    }

    void initGUI()
    {
        setRootVisible(false);
        addMouseListener(new MouseListener() {
            private boolean selectedOnPress;
            public void mouseClicked(MouseEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void mousePressed(MouseEvent e)
            {
                // This is our custom check to reject single clicks
                if (e.getClickCount() == 1) {
                    return;
                }
                if (!e.isConsumed())
                {
                    TreePath path = getClosestPathForLocation(e.getX(),e.getY());
                    if(path != null)
                    {
                        Object lastComponent = path.getLastPathComponent();
                        if(lastComponent instanceof DefaultMutableTreeNode)
                        {
                            DefaultMutableTreeNode node = (DefaultMutableTreeNode)lastComponent;
                            Object userObject = node.getUserObject();
                            if(userObject instanceof CertInfo)
                            {
                                CertInfo certInfo =(CertInfo)userObject;
                                showCertificate(certInfo.geteCertViewerCert().getX509Cert());
                            }
                        }
                    }
                }
                else
                {
                }
            }

            public void mouseReleased(MouseEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void mouseEntered(MouseEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void mouseExited(MouseEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
    }
    ReaderCertListTree(DefaultMutableTreeNode iRootNode)
    {
        super(iRootNode);
        initGUI();
    }


    private void showCertificate(X509Certificate x509Cert) {
        try {
            ECertViewerDialog.showCertificate(x509Cert.getEncoded());
        } catch (CertificateEncodingException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, CertSelectorBundle.getString("ReaderCertListTree.EncodingError")+e.getMessage());
        }
    }
}